﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

using FCNS.KeepMoney.Util;
using FCNS.KeepMoneyLibrary.Data;
using FCNS.KeepMoney.Forms;

namespace FCNS.KeepMoney.UI
{
    public partial class GtdViewDefault
    {
        public void InitTasks()
        {
            tasks.Clear();
            foreach (GtdEntryField gef in GtdEntryField.GetGtdEntryField(firstDate, lastDate, Program.AppConfig.App.ShowOverdueData))
                if (gtdGroups.ContainsKey(gef.GtdGroup.Id))
                    tasks.Add(gef);

            TaskSort();
        }

        private void TaskSort()
        {
            tasks.Sort(CompareDate);

            if (!Program.AppConfig.Gtd.SortByDate)
            {
                List<GtdEntryField> unFinish = tasks.FindAll(gef=> { return !gef.Finish; });
                tasks.RemoveRange(0, unFinish.Count);
                unFinish.Sort(CompareLevel);
                tasks.InsertRange(0, unFinish);
            }

            expired = 0;
            foreach (GtdEntryField task in tasks)
                if (!task.Finish && task.EndTime < DateTimeUtil.RemoveTime(DateTime.Now))
                    expired++;
        }

        private int CompareLevel(GtdEntryField obj1, GtdEntryField obj2)
        {
            int res = 0;
            if (obj1.Level > obj2.Level)
                res = -1;
            else
            {
                if (obj1.EndTime > obj2.EndTime)
                    res = 1;
                else if (obj1.EndTime < obj2.EndTime)
                    res =0;
            }
            return res;
        }

        private int CompareDate(GtdEntryField obj1, GtdEntryField obj2)
        {
            int res = 0;
            if ((obj1.Finish) && (obj2.Finish))
            {
                if (obj1.EndTime > obj2.EndTime)
                    res = -1;
                else if (obj1.EndTime < obj2.EndTime)
                    res = 1;
            }
            else if ((!obj1.Finish) && (obj2.Finish))
                res = -1;
            else if ((obj1.Finish) && (!obj2.Finish))
                res = 1;
            else
            {
                if (obj1.EndTime > obj2.EndTime)
                    res = 1;
                else if (obj1.EndTime < obj2.EndTime)
                    res = -1;
            }

            return res;
        }


        public void ModifiGtdGroups(GtdGroupField g, bool add)
        {
            if (add)
                gtdGroups[g.Id] = g;
            else
                gtdGroups.Remove(g.Id);
        }

        public void RemoveTask(GtdEntryField task) { }

        public GtdEntryField SelectedTask
        {
            get;
            set;
        }

        public System.DateTime Date
        {
            get;
            set;
        }

        public List<GtdEntryField> Tasks
        {
            get { return tasks; }
        }

        private string ShowDateForm(int y, int Y)
        {
            DateForm d = new DateForm();
            d.Location = new Point(y, Y);
            d.Init(selectedTask);
            d.ShowDialog();
            return d.SelectedDate.ToString(configApp.DateFormat);
        }

        private void OnNoteFormShow(GtdEntryField task)
        {
            TextForm f = new TextForm();
            f.Init("GTD", "GTD 任务备注编辑器", task.Note);
            f.UsedByMutliTextEdit();
            if (f.ShowDialog() == DialogResult.OK)
                task.Note = f.GetText;
        }

        private void FillRectangle(Graphics g, Color color,int x, int y, int width)//x=10
        {
            Pen penTask = new Pen(color);
            SolidBrush brush = new SolidBrush(color);

            g.DrawLine(penTask, x+4, y, width - 4, y);
            g.DrawLine(penTask, x + 3, y + 1, width - 3, y + 1);
            g.DrawLine(penTask, x + 2, y + 2, width - 2, y + 2);
            g.DrawLine(penTask, x + 1, y + 3, width - 1, y + 3);
            g.DrawLine(penTask, x, y + 4, width, y + 4);

            g.DrawLine(penTask, x + 4, y + 28 - 2, width - 4, y + 28 - 2);
            g.DrawLine(penTask, x + 3, y + 28 - 3, width - 3, y + 28 - 3);
            g.DrawLine(penTask, x + 2, y + 28 - 4, width - 2, y + 28 - 4);
            g.DrawLine(penTask, x + 1, y + 28 - 5, width - 1, y + 28 - 5);
            g.DrawLine(penTask, x, y + 28 - 6, width, y + 28 - 6);

            g.FillRectangle(brush, x, y + 5, width - 9, 17);

            penTask.Dispose();
            brush.Dispose();
        }

        void GtdViewDefault_MouseWheel(object sender, MouseEventArgs e)
        {
            if (!vScrollBar1.Visible)
                return;

            if (e.Delta > 0)
            {
                if (vScrollBar1.Value > 0)
                    vScrollBar1.Value--;
            }
            else
            {
                if (vScrollBar1.Value < vScrollBar1.Maximum)
                    vScrollBar1.Value++;
            }
        }
    }
}
